/*
 * Copyright (c) 2010, 2011
 *	Open Source Technologies Ltd.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the company nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COMPANY AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*-
 * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Klaus Klein and Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright (c) 1987, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#include "defs.h"

static void show_version __p((void));
static void show_usage   __p((void));

static char *process_name = NULL;

#if defined(WINDOWS)

#define BADCH  '?'
#define BADARG ':'
#define	EMSG   ""

static char *get_base_name __p((const char *path));
static int   get_option    __p((int argc, const char *argv[], const char *opts));

static int   option_index    = 1;
static int   option_option   = 0;
static char *option_argument = NULL;
static bool  option_reset    = false;

#else /* !WINDOWS */

#include <unistd.h>
#include <libgen.h>

#define get_base_name(path)          basename(path)
#define get_option(argc, argv, opts) getopt((argc), (argv), (opts))

#define option_index    optind
#define option_argument optarg

#endif /* !WINDOWS */

int
main(int argc, char *argv[])
{
	int ch;

	process_name = get_base_name(argv[0]);

	while (1) {
		ch = get_option(argc, argv, "hv");
		if (ch < 0)
			break;

		switch (ch) {
		case 'h':
			show_usage();
			return EXIT_SUCCESS;
		case 'v':
			show_version();
			return EXIT_SUCCESS;
		default:
			return EXIT_FAILURE;
		}
	}

	argc -= option_index;
	argv += option_index;

	return EXIT_SUCCESS;
}

static void
show_version(void)
{
	fprintf(
		stderr,
		"BLISS-64 Compiler, version 0.1.0\n\n"
		"Copyright (c) 2010, 2011,\n"
		"  Open Source Technologies Ltd. All rights reserved.\n"
		"Copyright (c) 1997, 2002,\n"
		"  The NetBSD Foundation, Inc. All rights reserved.\n"
		"Copyright (c) 1987, 1993, 1994,\n"
		"  The Regents of the University of California.  All rights reserved.\n\n"
	);
}

static void
show_usage(void)
{
	show_version();
	fprintf(
		stderr,
		"Usage:\n"
		"  %s [options] [files...]\n\n"
		"Options: \n"
		"  -v  Print version information and exit\n"
		"  -h  Print this help and exit\n",
		process_name
	);
}

#if defined(WINDOWS)

static char *
get_base_name(char *path)
{
	static char result[PATH_MAX];

	const char *p, *lastp;
	size_t len;

	/*
	 * If `path' is a null pointer or points to an empty string,
	 * return a pointer to the string ".".
	 */
	if ((path == NULL) || (*path == '\0')) {
		result[0] = '.';
		result[1] = '\0';

		return (result);
	}

	/* Strip trailing slashes, if any. */
	lastp = path + strlen(path) - 1;
	while (lastp != path && *lastp == '/')
		lastp--;

	/* Now find the beginning of this (final) component. */
	p = lastp;
	while (p != path && *(p - 1) != '/')
		p--;

	/* ...and copy the result into the result buffer. */
	len = (lastp - p) + 1 /* last char */;
	if (len > (PATH_MAX - 1))
		len = PATH_MAX - 1;

	memcpy(result, p, len);
	result[len] = '\0';

	return result;
}

static int
get_option(int argc, const char *argv[], const char *opts)
{
	static const char *place = EMSG;
	char *oli;

	assert(argv != NULL);
	assert(opts != NULL);

	/* update scanning pointer */
	if (option_reset || *place == 0) {
		option_reset = false;
		place = argv[option_index];
		if (option_index >= argc || *place++ != '-') {
			/* Argument is absent or is not an option */
			place = EMSG;
			return (-1);
		}
		option_option = *place++;
		if (option_option == '-' && *place == 0) {
			/* "--" => end of options */
			++option_index;
			place = EMSG;

			return -1;
		}
		if (option_option == 0) {
			/* Solitary '-', treat as a '-' option
			   if the program (eg su) is looking for it. */
			place = EMSG;
			if (strchr(opts, '-') == NULL)
				return -1;
			option_option = '-';
		}
	} else
		option_option = *place++;

	/* See if option letter is one the caller wanted... */
	if (option_option == ':' || (oli = (char *)strchr(opts, option_option)) == NULL) {
		if (*place == 0)
			++option_index;
		if (*opts != ':')
			fprintf(stderr, "%s: unknown option -- %c\n", process_name, option_option);
		return BADCH;
	}

	/* Does this option need an argument? */
	if (oli[1] != ':') {
		/* don't need argument */
		option_argument = NULL;
		if (*place == 0)
			++option_index;
	} else {
		/* Option-argument is either the rest of this argument or the
		   entire next argument. */
		if (*place)
			option_argument = (char *)place;
		else if (argc > ++option_index)
			option_argument = (char *)argv[option_index];
		else {
			/* option-argument absent */
			place = EMSG;
			if (*opts == ':')
				return (BADARG);
			fprintf(stderr, "%s: option requires an argument -- %c\n", process_name, option_option);

			return BADCH;
		}
		place = EMSG;
		++option_index;
	}

	/* return option letter */
	return (option_option);
}

#endif /* WINDOWS */
